package com.shundr;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.actionbarsherlock.view.Window;
import com.shundr.base.BaseActivity;
import com.shundr.common.adapter.ViewPagerAdapter;
import com.shundr.common.util.PrefConstants;
import com.shundr.common.util.PrefUtils;
import com.shundr.frame.util.GetVersionUtil;

/**
 * 引导页
 * GuideActivity
 * 
 * @author larry 2015-5-20 上午11:39:03 
 * @version 1.0.0
 *
 */
public class GuideActivity extends BaseActivity implements OnClickListener,
		OnPageChangeListener {
	private ViewPager viewPager;
	private ViewPagerAdapter vpAdapter;
	private ArrayList<ImageView> views;
	private Button btnStart;
	private int[] pics = { R.drawable.bg_guide_1, R.drawable.bg_guide_2,
			R.drawable.bg_guide_3, R.drawable.bg_guide_4};
	private ImageView[] points;
	private int currentIndex;
	
	private boolean isGuide = false;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 100:
				
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		isGuide = getIntent().getBooleanExtra("flag", false);
		initView();
		initData();
	}

	private void initView() {
		views = new ArrayList<ImageView>();
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		vpAdapter = new ViewPagerAdapter(views);
		btnStart = (Button)findViewById(R.id.btn_start);
		btnStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isGuide){
					finish();
				}else{
					PrefUtils.setPrefBoolean(mContext, PrefConstants.IS_FIRST, false);
					int versionCode=GetVersionUtil.getVersionCode(mContext);
					PrefUtils.setPrefInt(mContext, PrefConstants.VERSION_CODE, versionCode);
					startActivity(new Intent(mContext, MainActivity.class));
				}
			}
		});
	}

	private void initData() {
		viewPager.setAdapter(vpAdapter);
		viewPager.setOnPageChangeListener(this);
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT,
						LinearLayout.LayoutParams.MATCH_PARENT);

				for (int i = 0; i < pics.length; i++) {
					ImageView iv = new ImageView(mContext);
					iv.setLayoutParams(mParams);
//					iv.setScaleType(ScaleType.FIT_XY);
					iv.setBackgroundResource(pics[i]);
					views.add(iv);
				}

				vpAdapter.notifyDataSetChanged();
				initPoint();
			}
		}, 0);
		
	}

	private void initPoint() {
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bottom);
		points = new ImageView[pics.length];
		for (int i = 0; i < pics.length; i++) {
			points[i] = (ImageView) linearLayout.getChildAt(i);
			points[i].setEnabled(true);
			points[i].setOnClickListener(this);
			points[i].setTag(i);
		}

		currentIndex = 0;
		points[currentIndex].setEnabled(false);
	}
	
	@Override
	protected void onDestroy() {
		for(ImageView view : views){
			releaseImageView(view);
		}
		super.onDestroy();
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		setCurDot(arg0);
	}

	@Override
	public void onClick(View v) {
		int position = (Integer) v.getTag();
		setCurView(position);
		setCurDot(position);
	}

	private void setCurView(int position) {
		if (position < 0 || position >= pics.length) {
			return;
		}
		viewPager.setCurrentItem(position);
	}

	private void setCurDot(int positon) {
		if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
			return;
		}
		points[positon].setEnabled(false);
		points[currentIndex].setEnabled(true);

		currentIndex = positon;
		if(positon==pics.length-1){
			btnStart.setVisibility(View.VISIBLE);
		}else{
			btnStart.setVisibility(View.INVISIBLE);
		}
	}
}
